<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp" %>
<% 
 String operation = request.getParameter("op");
 String id = request.getParameter("id");
 %>
<!DOCTYPE html>
<html lang="en">
	<head>
		<%@ include file="/common/common-css.jsp" %>
		
		<!-- loading -->
		<link rel="stylesheet" href="/loan_manager/css/common-loading.css">
			
		<style>
			.table > th {
			  vertical-align: center;
			  background-color: grey;
			}
		</style>		
	</head>
	<body class="no-skin">
		
		<!-- loading -->
		<%@ include file="/common/common-loading.jsp"%>
		
		<div class="page-content">
			<div id="page-header" class="page-header">
				<h1>
					
				</h1>
			</div>
			<div class="page-content-area">
				<div class="row">
					<div class="col-xs-12 col-sm-12 widget-container-col">
						<div class="widget-box widget-color-blue">
							<!-- #section:custom/widget-box.options.collapsed -->
							<div class="widget-header widget-header-small">
								<h6 class="widget-title lighter">
									<i class="ace-icon glyphicon glyphicon-th-list"></i>编辑
								</h6>
	                            <div class="widget-toolbar no-border">
									<a href="#" data-action="collapse">
										<i class="ace-icon fa fa-chevron-up"></i>
									</a>
								</div>
							</div>
							
							<div class="widget-body">
								<div class="widget-main" >
									<form class="form-horizontal" id="channel_form" method="post" enctype="multipart/form-data" >
										<input type="hidden" name="id"  id="idself" value = "${ model.id}"placeholder="" class="col-xs-12 col-sm-6">
										<input type="hidden" id="slink" value = "${ model.spreadLink}"placeholder="" class="col-xs-12 col-sm-6">
										<table id="baseinfo" class="table table-bordered table-striped table-hover table-condensed table-responsive table">

											<tr>
												<th style="width:100px">产品ID</th>
												<td colspan="7">
													 <s:select name="prdId" id="prdId"
																list="%{codeList}" listKey="prdid"
																listValue="name" headerKey="" headerValue="--请选择--"
																cssClass="form-control">
													</s:select>
													</td>
											</tr>

											<tr>
												<th style="width:100px">推广链接</th>
													<td colspan="7">
														<input type="text" name="spreadLink"  id="spreadLink" placeholder=""  class="col-xs-12 col-sm-4" style="width:600px"/>
													</td>
											</tr>
											
										</table>
									</form>
								</div><!-- /.widget-main -->
							</div><!-- /.widget-body -->
						</div><!-- /.widget-box -->
					</div><!-- /.col-sm-12 -->
				</div><!-- /.row -->
				<div class="clearfix form-actions">
					<div class="col-md-offset-3 col-md-9">
						<button class="btn  btn-inverse  no-border " onclick="doCancel()" id="delete"> 
							<i class="ace-icon fa fa-undo bigger-160"></i>
							取消
						</button>
		
						&nbsp; &nbsp; &nbsp;
						<button id="btn-save" name="btn-save" class="btn  btn-success  no-border" type="button" onclick="doSave()">
							<i class="ace-icon fa fa-floppy-o bigger-160"></i>
							确定
						</button>
					</div>
				</div>
				</div>
			</div>
		</div>
	</body>
</html>

<%@ include file="/common/common-js.jsp" %>
<!-- loading js -->
<script type="text/javascript" src="${ctx}/js/common-loading.js"></script>
<script type="text/javascript" src="${ctx}/treasure/common_js/digital_utils.js?v=1"></script>

<script>

	//操作，0表示新增，1表示更新
	var operation = getQueryString('op');
	var prdId = getQueryString('prdId');
	var name = getQueryString('name');
	//var spreadLink = getQueryString('spreadLink');
	var spreadLink = $('#slink').val();
	var flag;
	//如果是更新，channelid
	var id = "<%=id%>";
		
	$(function(){
		$("#id").val(id);
		
		//标题
		if (operation == 0) {
			$("#page-header h1").html("新增渠道");
			$("#prdId").removeAttr("readonly"); 
			
			//$("#spread_link,#spreadLink").hide();
		} else if (operation == 1){
			$("#page-header h1").html("更新渠道");
			
			$("#prdId").val(prdId);
			$("#prdId").attr("readonly","readonly");
			$("#spreadLink").val(spreadLink);
			$("#spreadLink").attr("readonly","readonly");
		} else if (operation == 2){
			$("#page-header h1").html("删除渠道");
			$("#prdId").val(prdId);
			$("#spreadLink").val(spreadLink);
			$("#prdId").attr("readonly","readonly");
			$("#spreadLink").attr("readonly","readonly");
		}
	});
	
	function getQueryString(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	 	var r = window.location.search.substr(1).match(reg);
		if (r != null) return decodeURI(r[2]); return null;
		
	};
	
	/**
	 * 数据更新界面的方法
	 * @param {Object} data
	 */
	function initViewByData(data) {
		var channel = data.channel;
		$("#channel").val(channel);
		
		var channelname = data.channelname;
		$("#channelname").val(channelname);
	};
	
	function doCancel() {
		parent.parent['__extdialog_active'].dialog('close');
	};
	
	function doSave() {
		//数据校验
		
		
		var idself = $('#idself').val();
		var id = $('#prdId').val();
		
		//var isapp=$('input:radio[name="isapp"]:checked').val();
		
		var idJudgeurl = ctx + "/spreadchannel/isExist.action";
		
		var requestUrl;
		if (operation == 0) {
			
			var judge = $.ajax({
				type:"POST",
				url:idJudgeurl,
				data:{prdId:id,name:name},
				cache: false,
		        async : false
			}).responseText;
			
			
			if(judge == "true"){
				alert("ID已存在");
				return;
			}
			
			if (id == null || id.trim().length <= 0){
				alert("ID不能为空");
				return;
			}
			requestUrl = ctx + "/spreadchannel/addNewChannel.action?rd=" + new Date().getTime();
		} else if(operation == 1){

			if (id == null || id.trim().length <= 0){
				alert("ID不能为空");
				return;
			}
			
			var judge = $.ajax({
				type:"POST",
				url:ctx + "/spreadchannel/isUpdate.action",
				data:{id:idself,prdId:id},
				cache: false,
		        async : false
			}).responseText;
			if(judge == "true"){
				alert("ID已存在或数据无更新");
				return;
			}
			
			requestUrl = ctx + "/spreadchannel/updateChannel.action?id=" + idself +"&prdId=" + id +"&rd=" + new Date().getTime();
		} else{
			requestUrl = ctx + "/spreadchannel/dropChannel.action?prdId=" + id;
		}
		//请求地址
		var iconChooseForm = document.getElementById("channel_form");
		var formData = new FormData(iconChooseForm);
		var xhr = new XMLHttpRequest();
		xhr.onreadystatechange = function(){
			if(xhr.readyState==4) {
				if(xhr.status==200) {
					alert("数据保存成功");
					parent.parent['__extdialog_active'].dialog('close');
				    parent.main_frame.query_any();
				} else {
					alert("数据保存失败");
				}
				hideLoading();
			}
		};    
		xhr.open("POST" ,requestUrl , true);
		xhr.send(formData);
		showLoading();
	};
	

</script>	
